package cn.xorange.commons.system.service.impl;

import cn.hutool.http.HttpUtil;
import cn.xorange.commons.system.entity.common.SysArea;
import cn.xorange.commons.system.mapper.SysAreaMapper;
import cn.xorange.commons.system.utils.DictUtils;
import cn.xorange.commons.utils.cache.ICacheService;
import cn.xorange.commons.core.constants.SysConstants;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * @author : yangjian
 * date : 2023/11/4
 * description :
 */
@Service
public class SysAreaServiceImpl {

    @Autowired
    SysAreaMapper mapper;
    @Resource
    ICacheService cacheService;

    private static final String[] ZXS ={"110000","120000","310000","500000","810000","820000"};




    public void refreshArea(){
        String mapKey = DictUtils.getDictValue(cn.xorange.commons.system.constants.SysConstants.APP_KEY_CACHE,"qqMapKey");
        String url = cn.xorange.commons.system.constants.SysConstants.QQ_AREA + mapKey;
        String jsStr = HttpUtil.get(url);
        JSONObject obj = JSON.parseObject(jsStr);
        if(SysConstants.FLAG_SUCCESS.equals(obj.getString("status"))){
            JSONArray array = obj.getJSONArray("result");
            List<SysArea> pLst = array.getJSONArray(0).toList(SysArea.class);
            List<SysArea> cLst = array.getJSONArray(1).toList(SysArea.class);
            List<SysArea> aLst = array.getJSONArray(2).toList(SysArea.class);

            List<SysArea> newCList = new ArrayList<>();
            for(SysArea s : pLst){
                for(String id : ZXS) {
                    s.setParentId("0");
                    s.setLevel(0);
                    if(Objects.isNull(s.getName())){
                        s.setName(s.getFullname());
                    }

                    if (id.equals(s.getId())) {
                        SysArea zArea = new SysArea();
                        String zxsId = s.getId().substring(0,2) + "0100";
                        zArea.setId(zxsId);
                        zArea.setParentId(s.getId());
                        zArea.setLevel(1);
                        if(Objects.isNull(s.getName())){
                            zArea.setName(s.getFullname());
                        }else {
                            zArea.setName(s.getName());
                        }
                        zArea.setFullname(s.getFullname());
                        zArea.setPinyin(s.getPinyin());
                        zArea.setLat(s.getLat());
                        zArea.setLng(s.getLng());
                        newCList.add(zArea);
                    }
                    s.setParentId("0");
                    s.setLevel(0);
                }
            }

            List<SysArea> zy = new ArrayList<>();
            for (SysArea s : cLst){
                if(Objects.isNull(s.getName())){
                    s.setName(s.getFullname());
                }
                String sb = s.getId().substring(0,2);
                String sbId = sb + "0000";
                boolean isZ = false;
                for(String z : ZXS){
                    if(sbId.equals(z)){
                        isZ = true;
                        s.setLevel(2);
                        s.setParentId(sb + "0100");
                        zy.add(s);
                    }
                }
                if(!isZ){
                    s.setParentId(sbId);
                    s.setLevel(1);
                    newCList.add(s);
                }
            }

            for (SysArea s: aLst){
                if(Objects.isNull(s.getName())){
                    s.setName(s.getFullname());
                }
                String sb = s.getId().substring(0,4);
                s.setParentId(sb+"00");
                s.setLevel(2);
            }
            aLst.addAll(zy);

            List<SysArea> allList = new ArrayList<>();
            allList.addAll(pLst);
            allList.addAll(newCList);
            allList.addAll(aLst);

            mapper.truncate(allList.get(0));
            cacheService.delete(cn.xorange.commons.system.constants.SysConstants.AREA_CACHE);
            mapper.insertBath(allList);
            this.getSysAreaList();
        }

    }

    public JSONArray getSysAreaList(){
        JSONArray a = cacheService.getCacheObject(cn.xorange.commons.system.constants.SysConstants.AREA_CACHE);
        if(Objects.nonNull(a)){
            return a;
        }else {
            SysArea sysArea = new SysArea();
            sysArea.setLevel(0);
            List<SysArea> pList = mapper.selectList(sysArea);
            SysArea city = new SysArea();
            city.setLevel(1);
            List<SysArea> cList = mapper.selectList(city);
            SysArea area = new SysArea();
            area.setLevel(2);
            List<SysArea> aList = mapper.selectList(area);
            JSONArray array = new JSONArray();
            array.add(0, pList);
            array.add(1, cList);
            array.add(2, aList);
            cacheService.setCacheObject(cn.xorange.commons.system.constants.SysConstants.AREA_CACHE, array);
            return array;
        }
    }

    public SysArea getArea(String id){
        int i = 0;
        String last4 = id.substring(id.length() -4);
        if("0000".equals(last4)){
            i = 0;
        }else {
            String last2 = id.substring(id.length() -2);
            if("00".equals(last2)){
                i = 1;
            }else {
                i = 2;
            }
        }

        JSONArray array = cacheService.getCacheObject(cn.xorange.commons.system.constants.SysConstants.AREA_CACHE);
        SysArea[] areaList = array.getJSONArray(i).toArray(SysArea.class);
        for (SysArea a : areaList){
            if(a.getId().equals(id)){
                return a;
            }
        }
        return null;
    }
}
